import { listEvaluationVoByPageUsingPost } from '@/services/backend/evaluationController';
import type { ActionType, ProColumns } from '@ant-design/pro-components';
import { PageContainer, ProTable } from '@ant-design/pro-components';
import '@umijs/max';
import { useModel } from '@umijs/max';
import { Rate } from 'antd';
import React, { useRef } from 'react';

const TableList: React.FC = () => {
  const actionRef = useRef<ActionType>();

  const { initialState } = useModel('@@initialState');
  const currentUser = initialState?.currentUser;

  const columns: ProColumns<API.EvaluationVO>[] = [
    {
      title: 'id',
      dataIndex: 'id',
      valueType: 'text',
      hideInForm: true,
    },
    {
      title: '评估人',
      dataIndex: ['coach', 'username'],
      valueType: 'text',
      hideInSearch: true,
    },
    {
      title: '评估的训练项目',
      dataIndex: ['trainingPlan', 'planName'],
      valueType: 'text',
      hideInSearch: true,
    },
    {
      title: '评分',
      dataIndex: 'score',
      valueType: 'text',
      hideInSearch: true,
      render: (text) => {
        //@ts-ignore
        return <Rate value={text} disabled count={10} />;
      },
    },
    {
      title: '内容',
      dataIndex: 'content',
      valueType: 'textarea',
    },

    {
      title: '创建时间',
      sorter: true,
      dataIndex: 'createTime',
      valueType: 'dateTime',
      hideInSearch: true,
      hideInForm: true,
    },
    {
      title: '更新时间',
      sorter: true,
      dataIndex: 'updateTime',
      valueType: 'dateTime',
      hideInSearch: true,
      hideInForm: true,
    },
  ];

  return (
    <PageContainer>
      <ProTable<API.Evaluation>
        headerTitle={'查询表格'}
        actionRef={actionRef}
        rowKey="key"
        search={{
          labelWidth: 120,
        }}
        request={async (params, sort, filter) => {
          const sortField = Object.keys(sort)?.[0];
          const sortOrder = sort?.[sortField] ?? undefined;
          const { data, code } = await listEvaluationVoByPageUsingPost({
            ...params,
            sortField,
            sortOrder,
            ...filter,
            studentId: currentUser?.id,
          } as API.EvaluationQueryRequest);
          return {
            success: code === 0,
            data: data?.records || [],
            total: Number(data?.total) || 0,
          };
        }}
        columns={columns}
      />
    </PageContainer>
  );
};
export default TableList;
